package com.search;

import java.util.ArrayList;

/**
 * @author cjx
 * @create 2024/06/25
 * @description 基本查找/顺序查找
 */
public class BaseSearch {
    public static void main(String[] args) {
        int[] arr = { 2, 48, 17, 19, 32, 40, 55, 71, 99, 55 };
//        System.out.println(baseSearch(arr, 59));
        System.out.println(baseSearch1(arr, 55));
    }

    // 判断数组中是否包含给定的数字
    private static boolean baseSearch(int[] arr, int num) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) {
                return true;
            }
        }
        return false;
    }

    // 判断数组中是否包含给定的数字，并返回所有下标
    private static ArrayList<Integer> baseSearch1(int[] arr, int num) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) {
                list.add(i);
            }
        }
        return list;
    }
}
